$(function () {
    //变量抽取
    var client_dataGrid = $("#client_dataGrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var client_moveClient = $("#client_moveClient");
    var client_moveClientForm = $("#client_moveClientForm");


    client_dataGrid.datagrid({
        url: "/client/querySrouce.do",
        fitColumns: true,//列自适应
        fit: true,
        align: 'center',
        columns: [[
            {field: 'createDate', title: '创建日期', width: 100, align: 'center'},
            {
                field: 'state', title: '客户状态', width: 100, align: 'center', formatter: function (value) {
                return value == 1 ? "资源池客户" : "流失";
            }
            },
            {
                field: 'createEmp', title: '创建人', width: 100
            },
            {
                field: 'chargeEmp', title: '负责人', width: 100
            },
            {
                field: 'clientName', title: '客户名称', width: 100
            },
            {field: 'age', title: '年龄', width: 100, align: 'center'},
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                return value ? "男" : "女";
            }
            },
            {
                field: 'profession', title: '职业', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {
                field: 'salaryLevel', title: '薪资水平', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {field: 'tel', title: '电话', width: 100, align: 'center'},
            {field: 'email', title: '邮箱', width: 100, align: 'center'},
            {field: 'clientAddress', title: '客户地址', width: 100, align: 'center'},
            {
                field: 'clientSource', title: '客户来源', width: 100, align: 'center', formatter: function (value) {
                return value ? value.name : ""
            }
            },
            {field: 'qq', title: 'QQ', width: 100, align: 'center'},
            {field: 'idCard', title: '身份证', width: 100, align: 'center'}
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    })
    client_dialog.dialog({
        width: 270,
        height: 430,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            client_form.form("clear")
        },
        resizeable:true
    })
    client_moveClient.dialog({
        width: 270,
        height: 260,
        closed: true,
        buttons: "#btns_moveClient",
        onClose: function () {
            client_moveClientForm.form("clear")
        },
        resizeable:true
    })
    //方法抽成对象统一管理
    var methodObj = {//添加按钮
        absorb: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要吸纳的客户', 'info');
            } else {
                $.messager.confirm("提示", "是否要将 " + row.clientName + " 作为你负责的客户?", function (r) {
                    if (r) {
                        $.get("/client/absorbClient.do", {clientId: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '操作成功', 'info', function () {
                                    client_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert('提示', data.msg, 'error')
                            }
                        }, "json")
                    }
                })
            }
        },
        //编辑按钮
        edit: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
            } else {
                row["profession.id"] = row.profession.id;
                row["clientSource.id"] = row.clientSource.id;
                row["salaryLevel.id"] = row.salaryLevel.id;
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "编辑");
                client_dialog.dialog("open");
            }
        },
        reload: function () {
            client_dataGrid.datagrid("load");
        },
        save: function () {
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            client_dialog.dialog("close");
                            client_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            })
        },
        //取消按钮
        cancel: function () {
            client_dialog.dialog("close")
        },
        searchForm: function () {
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            var idCard = $("[name=idCard]").val();
            var tel = $("[name=tel]").val();
            var chargeEmpName = $("[name=chargeEmpName]").val();
            var clientName = $("[name=clientName]").val();
            var gender = $(".gender_combobox").combobox('getValue');

            client_dataGrid.datagrid('load', {
                beginTime: beginTime,
                endTime: endTime,
                idCard: idCard,
                endTime: endTime,
                tel: tel,
                chargeEmpName: chargeEmpName,
                clientName: clientName,
                gender: gender
            });
        },
        //清空查询框
        clearSearchForm: function () {
            $("#beginTime").textbox("setValue", '');
            $("#endTime").textbox("setValue", '');
            $("#idCard").textbox("setValue", '');
            $("#tel").textbox("setValue", '');
            $("#chargeEmpName").textbox("setValue", '');
            $("#clientName").textbox("setValue", '');
            $(".gender_combobox").combobox('setValue', '');
        }
    }

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});